#region Using Directives

using System;
using System.Web;
using System.Web.Caching;
using System.Threading;

#endregion Using Directives

namespace SharePointStu.SharePoint.General.Cache
{
    /// <summary>
    /// Cache manager class.
    /// </summary>
    public sealed class CacheManager
    {
        #region Private Members

        /// <summary>
        /// Private singleton member
        /// </summary>
        static volatile CacheManager _instance;

        /// <summary>
        /// Private locking object.
        /// </summary>
        static readonly object syncRoot = new object();

        #endregion Private Members

        #region Initialisation

        /// <summary>
        /// Private constructor for singleton class.
        /// </summary>
        private CacheManager() { }

        #endregion Initialisation

        #region Public Properties

        /// <summary>
        /// Gets the singleton Instance object.
        /// </summary>
        public static CacheManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (syncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new CacheManager();
                        }
                    }
                }
                return _instance;
            }
        }

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// Store a value into the cache using the given key name.
        /// </summary>
        /// <param name="key">Cache key name</param>
        /// <param name="value">Cache value</param>
        /// <param name="expire">Cache expire date and time</param>
        /// <param name="priority">Cache priority level</param>
        public void Add(string key, object value, DateTime expire, CacheItemPriority priority)
        {
            Delete(key);
            HttpContext.Current.Cache.Add(key, value, null, expire, TimeSpan.Zero, priority, null);
        }

        /// <summary>
        /// Retrieve the cache value for a given key.
        /// </summary>
        /// <param name="key">Cache key to retrieve</param>
        /// <returns>object: cache value matching key provided.</returns>
        public object GetValue(string key)
        {
            object result = null;
            if (HttpContext.Current != null)
            {
                result = HttpContext.Current.Cache[key];
            }
            return result;
        }

        /// <summary>
        /// Delete the cache item matching the key provided.
        /// </summary>
        /// <param name="key">Key of cache item to delete.</param>
        public void Delete(string key)
        {
            if (GetValue(key)!=null)
            {
                HttpContext.Current.Cache.Remove(key);
            }
        }

        #endregion Public Methods
    }
}
